﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
using EvidenceManagement.Workstation.Security;
using EvidenceManagement.PresentationModel.Common;
using EvidenceManagement.ViewModel.Common;
using Autofac;
using Autofac.Core;

namespace EvidenceManagement.Workstation
{
    public partial class frmMainForm : Form, IMasterView
    {
        private int childFormNumber = 0;
        private readonly List<IView> views = new List<IView>();
        private readonly IMasterPresenter presenter;

        public frmMainForm()
        {
            InitializeComponent();

            presenter = AutofacContainerBuilder.Container.Resolve<IMasterPresenter>();
        }

        private void frmMainForm_Load(object sender, EventArgs e)
        {
            presenter.OnLoad();
            presenter.LoadLogin(new frmLogin(this));
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();            
        }


        #region IMasterView Members

        public void LoadChildView(IView view)
        {
            if (view is Form)
            {
                var existingView = this.MdiChildren.FirstOrDefault(form => form.Name == (view as Form).Name 
                    && (form is IView));

                if (existingView == null)
                {
                    (view as Form).MdiParent = this;
                }
                else
                {
                    view = (existingView as IView);
                }

                view.ShowView();                
            }
        }

        #endregion
    }
}
